local sidao = fk.CreateSkill {

  name = "joy__sidao",

  tags = {  },

}



sidao:addEffect(fk.CardUseFinished, {
  name = "joy__sidao",
  anim_type = "offensive",
  events = {fk.CardUseFinished},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(sidao.name) and player.phase == Player.Play and not player:isKongcheng() and
      player:usedSkillTimes(sidao.name, Player.HistoryPhase) == 0 then
      return self.joysidao_tos and #self.joysidao_tos > 0
    end
  end,
  on_cost = function(self, event, target, player, data)  --TODO: target filter
    local tars = {}
    for _, pid in pairs(self.joysidao_tos) do
      if #player.room:getPlayerById(pid):getCardIds("hej") > 0 then
        table.insertIfNeed(tars,pid)
      end
    end
    if #tars <= 0 then return end
    local tar = player.room:askForChoosePlayers(player,tars,1,1,"伺盗：你可以获得一名相同目标区域内的一张牌",sidao.name,true)
    if #tar > 0 then
      event:setCostData(self, tar[1])
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local p = room:getPlayerById(event:getCostData(self))
    if #p:getCardIds("hej") > 0 then
      local id = room:askForCardChosen(player,p,"hej",sidao.name,"选择"..Fk:translate(p.general).."区域内的一张牌获得之")
      room:obtainCard(player, id, false, fk.ReasonPrey)
    end
  end,
})
sidao:addEffect(fk.TargetSpecified, {
  refresh_events = {fk.TargetSpecified},
  can_refresh = function(self, event, target, player, data)
    return target == player and player:hasSkill(sidao.name) and player.phase == Player.Play and data.firstTarget
  end,
  on_refresh = function(self, event, target, player, data)
    self.joysidao_tos = {}
    local mark = player:getMark("joy__sidao-phase")
    if mark ~= 0 and #mark > 0 and #data:getAllTargets() > 0 then
      for _, id in ipairs(data:getAllTargets()) do
        if table.contains(mark, id) then
          table.insert(self.joysidao_tos, id)
        end
      end
    end
    if #data:getAllTargets() > 0 then
      mark = data:getAllTargets()
      table.removeOne(mark, player.id)
    else
      mark = 0
    end
    player.room:setPlayerMark(player, "joy__sidao-phase", mark)
  end,
})

return sidao